<template>
	<view class="searchPage">
		<view class="searchContent">
			<u-input v-model="keyWord" :focus="true" shape="circle" inputAlign="center" placeholder="请输入关键词"
				@confirm="doSearch" clearable>
				<view slot="suffix" class="iconClass" @click="doSearch">
					<u-icon name="search" color="#004DA1" size="52rpx"></u-icon>
				</view>
			</u-input>
		</view>
		<view class="historySearch" v-if="historyKey.length &&  !list.length">
			<view class="title">历史搜索</view>
			<view class="itemContent">
				<view v-for="(item,index) in historyKey" :key="index" class="historyItem" v-if="index<5"
					@click="searchHis(item)">
					{{item}}
				</view>
			</view>
		</view>
		<view class="newsDetial">
			<u-empty mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png"
				v-if="!list.length && !isLoading && isLoad" text="暂无数据"></u-empty>
			<view class="searchList">
				<view v-for="(item,index) in list" :key="index" @click="goDetail(item)">
					<view v-if="item.type==1 || item.type==2" class="solutionList">
						<view class="solutionInfo">
							<view class="solutionTitle">{{item.name}}</view>
							<view class="detailInfo">
								<view>{{getReleaseTime(item.create_time)}}</view>
								<view>{{item.readingVolume}}阅读</view>
							</view>
						</view>
						<view class="solutionImage">
							<u--image :src="item.images?(baseUrl+'/sys/common/static/'+item.images):''" shape="square"
								mode="aspectFill" width="208rpx" height="150rpx" radius="8rpx">
							</u--image>
						</view>
					</view>
					<view v-if="item.type==3" class="list">
						<view class="itemTop">
							<view class="topLeft">
								<view class="title">{{item.name}}</view>
								<view class="talk">
									<view v-for="(talk,num) in item.topicName?item.topicName.split('，'):[]" :key="num"
										class="talkItem">
										#{{talk}}
									</view>
								</view>
							</view>
							<view class="topRight">
								<u--image
									:src="item.images?(baseUrl+'/sys/common/static/'+item.images.split(',')[0]):''"
									shape="square" mode="aspectFill" width="208rpx" height="150rpx"
									bgColor="transparent" radius="8rpx"></u--image>
							</view>
						</view>
						<view class="itemBottom">
							<view class="operate">
								<view class="item">
									<u--image src="/static/community/4.png" shape="square" mode="aspectFill"
										width="30rpx" height="24rpx"></u--image>
									<view class="count">{{item.readingVolume||0}}</view>
								</view>
								<view class="item">
									<u--image src="/static/community/5.png" shape="square" mode="aspectFill"
										width="26rpx" height="26rpx"></u--image>
									<view class="count">{{item.replyCount||0}}</view>
								</view>
							</view>
							<view class="createtime">
								{{getReleaseTime(item.create_time)}}
							</view>
						</view>
					</view>
					<view v-if="item.type==4" class="studyList">
						<view class="image">
							<u--image :src="baseUrl+'/sys/common/static/'+item.images" shape="square" mode="aspectFill" width="670rpx"
								height="226rpx" radius="8rpx 8rpx 0 0">
							</u--image>
							<view class="movieStatus">
								<u--image src="/static/growup/1.png" shape="square" mode="aspectFill" width="88rpx"
									height="88rpx" radius="12rpx">
								</u--image>
							</view>
						</view>
						<view class="studyInfo">
							<view class="title">{{item.name}}</view>
							<view class="statics">
								<view>{{item.topicName}}</view>
								<view class="total">累计：{{item.readingVolume||0}}</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<u-loadmore v-if="list.length && isLoad" :status="isFinish?'nomore':'loading'" loading-text="正在加载"
				loadmore-text="加载更多" nomore-text="没有更多了" color="#d0d0d0" />
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				keyWord: "",
				historyKey: [],
				searchKey: "",
				baseUrl: this.$api.baseUrl,
				list: [],
				pageNo: 1,
				pageSize: 10,
				total: 0,
				isFinish: false,
				isLoading: false,
				isLoad: false,
			}
		},
		computed: {
			getReleaseTime(date) {
				return function(date) {
					if (date) {
						if (typeof date === 'number') {
							return new Date(date).format("YYYY-MM-dd hh:mm")
						} else {
							return new Date(date.replace(/\-/g, "/")).format("YYYY-MM-dd hh:mm")
						}
					} else {
						return ""
					}
				}
			},
		},
		onReachBottom() {
			if (!this.isLoading && !this.isFinish) {
				this.pageNo++;
				this.initData();
			}
		},
		onLoad() {
			uni.getStorage({
				key: "historyKey",
				success: (res => {
					this.historyKey = res.data;
				})
			})
		},
		methods: {
			doSearch() {
				if (this.keyWord.trim()) {
					//存缓存
					this.keyWord = this.keyWord.trim();
					if (this.historyKey.indexOf(this.keyWord) == -1) {
						this.historyKey.unshift(this.keyWord)
						uni.setStorage({
							key: 'historyKey',
							data: this.historyKey
						})
					}
					//多次点击
					if (this.keyWord !== this.searchKey && !this.isLoading) {
						this.searchKey = this.keyWord;
						this.initData(true);
					}
				} else {
					uni.showToast({
						icon: "none",
						title: "请输入关键词"
					})
				}
			},
			searchHis(item) {
				this.keyWord = item;
				if (item !== this.searchKey && !this.isLoading) {
					this.searchKey = item;
					this.initData(true);
				}
			},
			initData(isRefresh) {
				if (isRefresh) {
					this.isFinish = false;
					if (!this.isLoading) {
						this.pageNo = 1;
						this.list = [];
					}
				}
				this.isLoading = true;
				Promise.all([
					this.$request.get(this.$api.searchIndex, {
						title: this.searchKey,
						pageNo: this.pageNo,
						pageSize: this.pageSize
					}),
				]).then(res => {
					if (!this.isLoad) {
						uni.showToast({
							icon: "success",
							title: "查询成功"
						})
						this.isLoad = true;
					}
					uni.hideLoading()
					this.isLoading = false;
					this.list = this.list.concat(res[0].result.records)
					this.total = res[0].result.total;
					if (this.pageNo * this.pageSize < this.total) {
						this.isFinish = false
					} else {
						this.isFinish = true
					}
				})
			},
			goDetail(item) {
				if (item.type == 1 || item.type == 2) {
					uni.navigateTo({
						url: "../solutionDetial/solutionDetial?id=" + item.id,
					})
				} else if (item.type == 3) {
					uni.navigateTo({
						url: "/pages/postDetail/postDetail?id=" + item.id
					})
				} else if (item.type == 4) {
					uni.navigateTo({
						url: "/pages/studyDetail/studyDetail?id=" + item.id
					})
				}

			}
		}
	}
</script>

<style lang="scss">
	.searchPage {
		.searchContent {
			padding: 0rpx 40rpx 26rpx;
			background-color: #FAFAFA;

			.u-input {
				height: 40rpx;
				line-height: 40rpx;
				font-size: 24rpx;
			}

			.iconClass {
				border-left: 1rpx solid #D0D0D0;
				padding: 0 0 0 38rpx;
			}
		}

		.historySearch {
			padding: 0rpx 40rpx 26rpx;

			.title {
				font-size: 30rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 600;
				color: #333333;
			}

			.itemContent {
				display: flex;
				flex-wrap: wrap;
				justify-content: flex-start;

				.historyItem {
					font-size: 26rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #666666;
					height: 50rpx;
					line-height: 50rpx;
					border: 1rpx solid #D0D0D0FF;
					border-radius: 25rpx;
					padding: 0 20rpx;
					margin-right: 20rpx;
					margin-top: 20rpx;
				}
			}
		}

		.searchList {
			display: flex;
			flex-direction: column;
			padding: 0rpx 40rpx;

			.solutionList {
				background: #FFFFFF;
				box-shadow: inset 0px 0px 0px 1rpx rgba(230, 230, 230, .5);
				border-radius: 20rpx;
				padding: 30rpx;
				display: flex;
				flex-wrap: nowrap;
				margin-bottom: 20rpx;

				.solutionInfo {
					display: flex;
					justify-content: space-between;
					flex-direction: column;
					width: calc(100% - 208rpx);
					padding-right: 20rpx;
					font-family: PingFang SC-Bold, PingFang SC;
					font-weight: bold;
					color: #333333;
					font-size: 28rpx;

					.solutionTitle {
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-line-clamp: 2;
						-webkit-box-orient: vertical;
					}

					.detailInfo {
						display: flex;
						flex-wrap: nowrap;
						justify-content: space-between;
						flex-shrink: 0;
						padding-right: 20rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #999999;
						font-size: 26rpx;
					}
				}
			}

			.list {
				display: flex;
				flex-direction: column;
				padding: 30rpx 20rpx 24rpx;
				background: #FFFFFF;
				box-shadow: inset 0px 0px 0px 1rpx rgba(230, 230, 230, .5);
				border-radius: 10px 10px 10px 10px;
				margin-bottom: 20rpx;

				.itemTop {
					display: flex;
					flex-wrap: nowrap;
					justify-content: flex-start;
					padding-bottom: 20rpx;

					.topLeft {
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						width: 100%;

						.title {
							font-family: PingFang SC-Bold, PingFang SC;
							font-weight: bold;
							color: #333333;
							font-size: 28rpx;
							letter-spacing: 1rpx;
							overflow: hidden;
							text-overflow: ellipsis;
							display: -webkit-box;
							-webkit-line-clamp: 3;
							-webkit-box-orient: vertical;
							padding-right: 20rpx;
						}

						.talk {
							font-family: PingFang SC-Regular, PingFang SC;
							font-weight: 400;
							color: #004DA1;
							font-size: 26rpx;
							display: flex;
							flex-wrap: nowrap;
							overflow: auto;
							margin-right: 20rpx;

							.talkItem {
								flex-shrink: 0;
							}
						}
					}

					.topRight {
						flex-shrink: 0;
					}
				}

				.itemBottom {
					display: flex;
					flex-wrap: nowrap;
					justify-content: space-between;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #999999;
					font-size: 26rpx;

					.operate {
						display: flex;
						flex-wrap: nowrap;

						.item {
							display: flex;
							flex-wrap: nowrap;
							align-items: center;
							padding-right: 40rpx;

							.count {
								padding-left: 10rpx;
							}
						}
					}
				}
			}

			.studyList {
				display: flex;
				flex-direction: column;
				background-color: #fff;
				margin-bottom: 30rpx;
				border-radius: 8rpx;
				justify-content: center;

				.image {
					position: relative;

					.movieStatus {
						position: absolute;
						left: calc(50% - 44rpx);
						top: calc(50% - 44rpx);
					}
				}

				.studyInfo {
					padding: 24rpx 24rpx 20rpx;

					.title {
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #333333;
						font-size: 26rpx;
						line-height: 36rpx;
						height: 72rpx;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-line-clamp: 2;
						-webkit-box-orient: vertical;
					}

					.statics {
						font-size: 26rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #666666;
						display: flex;
						flex-wrap: nowrap;
						justify-content: space-between;
						margin-top: 20rpx;

						.total {
							font-family: PingFang SC-Regular, PingFang SC;
							font-weight: 400;
							color: #999999;
						}
					}
				}
			}
		}
	}
</style>
